<template>
  <a-spin :spinning="confirmLoading" class="tabkey-box">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
            <div class="top-yb">
             <a-col :span="8">
              <a-form-item label="医保目录搜索">
                <JSelectInput name="西药"  listUrl="wh/drugWestern/miSearch"   :columns="gyscolumns" :disabled="yibaoswitch == 'N' || YXSdisableSubmit" style="width:100%" placeholder="名称/助记号/编码、模糊匹配"  @change="JZSearch" :queryParamCode="['keyword','approvalNumber','code','manufacturer','name','pinyinCode']" :queryParamText="['关键字','批准文号','编码','生产企业','名称','助记码']"></JSelectInput>
              </a-form-item>
             </a-col>
             <a-col :span="8">
              <div style="display:flex;align-items:center">
                  <a-form-item label="医保对照">
                <JSwitch v-model="yibaoswitch" :disabled="YXSdisableSubmit" @change="ybdzChange"></JSwitch>
              </a-form-item>
              <div class="xianjiainfo">
                <div>
                  <span class="red">限价:</span>
                  <span>{{model.lmtPrice || '--'}}</span>
                </div>
                <div>
                  <span class="red">限制使用区划:</span>
                  <span>{{model.lmtUsedAreas || '--'}}</span>
                </div>
              </div>
              </div>
              </a-col>
            </div>
          <a-col :span="8">
            <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="助记码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['mnemonic',{}]"  placeholder="请输入助记码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['code',{rules:[ { required: true, message: '请输入!'}]}]" :disabled="getConfigStatus('generateDrugCode') || YXSdisableSubmit ?true:false" placeholder="请输入编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="零售价格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :disabled="YXSdisableSubmit" :precision="2" v-decorator="['retailPrice',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入零售价格" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="收费等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input  v-decorator="['miLevel',{}]" disabled placeholder="请输入收费等级"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="包装数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number style="width: 100%;" v-decorator="['packageQuantity',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入包装数量"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="批准文号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="ybdz || YXSdisableSubmit" v-decorator="['approvalNumber',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入批准文号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="采购价格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number style="width: 100%;" :precision="4" v-decorator="['purchaseUnitPrice',{'initialValue':'0'}]" placeholder="请输入采购价格"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="剂量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number style="width: 100%;"  v-decorator="['dose',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入剂量"></a-input-number>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="8">
            <a-form-item label="总剂量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number style="width: 100%;" v-decorator="['specification',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入规格"></a-input-number>
              
            </a-form-item>
          </a-col> -->
          <a-col :span="8">
            <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input style="width: 100%;" v-decorator="['specificationDescribe',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入规格"></a-input>
              
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择开始日期" @change="setTimeNext(beginSteps)" v-decorator="['startDate']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择结束日期" @change="setTimeNext(beginSteps+1)" v-decorator="['endDate']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="生产厂家" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input  v-decorator="['manufacturer',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入生产厂家"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="包装单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag :async="true" :triggerChange="true" placeholder="请选择"  v-decorator="['packUnit',{rules:[ { required: true, message: '请选择!'}]}]" dict="pack_unit">
              </j-search-select-tag>
            </a-form-item>
          </a-col>
         
          <a-col :span="8">
            <a-form-item label="剂量单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag :async="true" :triggerChange="true" placeholder="请选择"  v-decorator="['doseUnit',{rules:[ { required: true, message: '请选择!'}]}]" dict="drug_dose_unit">
              </j-search-select-tag>
            </a-form-item>
          </a-col>
          
          <a-col :span="8">
            <a-form-item label="剂型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag :async="true" :triggerChange="true" placeholder="请选择"  v-decorator="['dosageForm',{rules:[ { required: true, message: '请选择!'}]}]" dict="drug_dosform">
              </j-search-select-tag>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="单品提成" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :disabled="YXSdisableSubmit" style="width: 100%;" v-decorator="['singleCommission']" placeholder="请输入单品提成"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="库存上限" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number style="width: 100%;" :min="0" v-decorator="['stockLimitUp',validatorRules.stockLimitUp]" placeholder="请输入库存上限"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="库存下限" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number style="width: 100%;" :min="0" v-decorator="['stockLimitDown',validatorRules.stockLimitDown]" placeholder="请输入库存下限"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="处方药标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag :isHideOne="true" :triggerChange="true" v-decorator="['prescriptionFlag',{rules:[ { required: true, message: '请选择!'}]}]" dictCode="rx_flag" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="启用状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag :isHideOne="true" :triggerChange="true" v-decorator="['enabledFlag',{'initialValue':'1'}]" dictCode="depart_status" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="加成比例：" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <div class="jcblbox" style="display:flex;align-items:center;gap:5px">
                <j-dict-select-tag :isHideOne="true" :disabled="YXSdisableSubmit" v-model="model.markupRatioFlag" dictCode="RatioFlag_dict" placeholder="是否开启加成比例"/>
                <a-input-number ref="jcbl" :disabled="YXSdisableSubmit" v-if="model.markupRatioFlag+'' === '1'" style="width: 100%;" :min="1" v-decorator="['markupRatio',{rules:[ { required: true, message: '请输入加成比例!'}]}]" placeholder="请输入加成比例"></a-input-number>
              </div>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="无溯源码标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag :isHideOne="true" @change="noSymChange" :triggerChange="true"  v-decorator="['noTraceFlag',{'initialValue':'0',rules:[ { required: true, message: '请选择无溯源码标识!'}]}]" dictCode="no_trace_flag" />
            </a-form-item>
          </a-col>
          <template v-if="!isNoSym">
            <a-col :span="8">
              <a-form-item label="药品标识码" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input-number style="width: 100%;" :min="0" v-decorator="['identificationCode',{rules:[{validator: validSeven}]}]" placeholder="请输入药品标识码(溯源码前七位)"></a-input-number>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="溯源码拆零" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-dict-select-tag :isHideOne="true" @change="chailingChange" :triggerChange="true"  v-decorator="['trdnFlag',{'initialValue':'0'}]" dictCode="trdn_flag" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="溯源码拆零数量 " :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input-number style="width: 100%;" :disabled="!isChaiLing" v-decorator="['tracePackageQuantity',{'initialValue':'1',rules:[ { required: true, message: '请输入溯源码包装数量!'}]}]" placeholder="请输入溯源码包装数量"></a-input-number>
              </a-form-item>
            </a-col>
            <a-col :span="8">
            <a-form-item label="单次销售单位-？？" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag :async="true" :triggerChange="true" placeholder="请选择"  v-decorator="['doseUnit',{}]" dict="drug_dose_unit">
              </j-search-select-tag>
            </a-form-item>
            </a-col>
            <a-col :span="8">
            <a-form-item label="毒理分类-？？" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag :async="true" :triggerChange="true" placeholder="请选择"  v-decorator="['doseUnit',{}]" dict="drug_dose_unit">
              </j-search-select-tag>
            </a-form-item>
            </a-col>
            <a-col :span="8">
            <a-form-item label="抗 菌 素-？？" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag :async="true" :triggerChange="true" placeholder="请选择"  v-decorator="['doseUnit',{}]" dict="drug_dose_unit">
              </j-search-select-tag>
            </a-form-item>
          </a-col>
          </template>
        </a-row>
          <div class="yb-form" v-if="yibaobotshow && false">
            <a-row :span="24">
              <a-col :span="4">
                <a-form-item label="医保等级" :labelCol="{span:10}" :wrapperCol="{span:14}">
                  <a-input disabled v-decorator="['level']" placeholder=""></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="4">
                <a-form-item label="医保限价" :labelCol="{span:10}" :wrapperCol="{span:14}">
                  <a-input disabled  v-decorator="['priceLimit']" placeholder=""></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="职工-自付比例"  :labelCol="{span:10}" :wrapperCol="{span:14}">
                  <a-input disabled v-decorator="['employeeSelfPaymentRatio']" placeholder=""></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="居民-自付比例"  :labelCol="{span:10}" :wrapperCol="{span:14}">
                  <a-input disabled v-decorator="['residentSelfPaymentRatio']" placeholder=""></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="24">
                <a-form-item label="限制说明：限制用药" >
                  <a-input disabled type="textarea"  v-decorator="['restriction']" placeholder=""></a-input>
                </a-form-item>
              </a-col>
            </a-row>
          </div>
         
         
          <!-- <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col> -->
       
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JSwitch from '@/components/jeecg/JSwitch'
  import JDate from '@/components/jeecg/JDate'  
  import JSelectInput from '@comp/jeecgbiz/JSelectInput'
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  import { keyDownMinxin } from '@/mixins/keyDownMinxin' 
  import { yaodianFormMinxin } from '@/mixins/yaodianFormMinxin'
  export default {
    name: 'WhDrugWesternForm',
    mixins:[yaodianFormMinxin,keyDownMinxin],
    components: {
      JFormContainer,
      JDate,
      JSwitch,
      JSelectInput,
      JSearchSelectTag
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      //有销售禁用
      YXSdisableSubmit:{
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      
      return {
        yibaobotshow:false,
        keywords:'',
        yibaoswitch:'N',
        ybdz:false,
        form: this.$form.createForm(this),
        model: {},
        gyscolumns: [
          { title: '医保编码', dataIndex: 'medicalCatalogCode', align: 'center', width: '55%' },
          { title: '医保名称', dataIndex: 'medicalCatalogName', align: 'center', width: '45%' },
          { title: '名称', dataIndex: 'name', align: 'center', width: '45%' },
          { title: '批准文号', dataIndex: 'approvalNumber', align: 'center', width: '45%' },
          { title: '生产厂家', dataIndex: 'manufacturer', align: 'center', width: '45%' },
          { title: '剂量', dataIndex: 'dose', align: 'center', width: '45%' },
          { title: '包装单位', dataIndex: 'packUnit', align: 'center', width: '45%' },
          { title: '零售价格', dataIndex: 'retailPrice', align: 'center', width: '45%' },
          { title: '药品规格', dataIndex: 'drugSpecification', align: 'center', width: '45%' },
          { title: '包装数量', dataIndex: 'packageQuantity', align: 'center', width: '45%' },
          { title: '收费等级', dataIndex: 'miLevel', align: 'center', width: '45%' },
          { title: '限价', dataIndex: 'lmtPrice', align: 'center', width: '45%' },
          { title: '限制使用区划', dataIndex: 'lmtUsedAreas', align: 'center', width: '45%'},
        ],
        labelCol: {
          lg: { span: 8 },
          xxl: { span: 8 },
        },
        wrapperCol: {
          lg: { span: 16 },
          xxl: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          splitFlag: {
            rules: [
              { required: true, message: '请输入拆分标志 0:不可拆分 1:可以拆分!'},
            ]
          },
          prescriptionFlag: {
            rules: [
              { required: true, message: '请输入处方药标识 0:非处方 1:处方!'},
            ]
          },
          enabledFlag: {
            rules: [
              { required: true, message: '请输入启用标识 0:禁用 1:启用!'},
            ]
          },
          delFlag: {
            rules: [
              { required: true, message: '请输入删除标识 0:未删除 1:已删除!'},
            ]
          },
          stockLimitDown:{
            rules: [{validator: this.validateStockLimitDown}]
          },
          stockLimitUp:{
            rules: [{validator: this.compareStockLimitUp}]
          }
        },
        url: {
          add: "/wh/drugWestern/add",
          edit: "/wh/drugWestern/edit",
          saleEdit: "/wh/drugWestern/saleEdit",
          queryById: "/wh/drugWestern/detail",
          repeatCheck:'wh/drugWestern/repeatCheck'
        },
        repeatForm:{
          project:'1'
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            console.log('000000');
            this.keywords = '';
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      },
      beginSteps(){
         var a = 7 
         if(this.YXSdisableSubmit){
          a = 5
         }
         return a
      }
    },
    mounted () {

      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      validSeven(rule, value, callback){
        if (value) {
          if (!/^\d{7}$/.test(value)) {
            callback('输入值必须是7位数字！');
          }
        }
        callback();
      },
      validateStockLimitDown  (rule, value, callback) {
        const form = this.form;
        const stockLimitUp=form.getFieldValue('stockLimitUp');

        
        if (value) {
          if (value >= stockLimitUp) {
            callback('下限不能大于上限！');
          }
          form.validateFields(['stockLimitUp'], { force: true })
        }
        callback();
      },
      compareStockLimitUp  (rule, value, callback) {
        const form = this.form;
        if (value && value <= form.getFieldValue('stockLimitDown')) {
          // callback('上限不能小于下限！');
          callback('下限不能大于上限！');
        }else {
          callback()
        }
      },
      getCode(){
        if(this.getConfigStatus('generateDrugCode')){
          getAction('wh/drugWestern/getCode',{}).then((res)=>{
            if(res.success){
              this.form.setFieldsValue({'code':res.result.code})
            }
          });
        }
      },
      //医保限价查询
      JZSearch(val){
        console.log(val,'医保限价查询')
        //限价信息
        this.model.lmtPrice = val[0].lmtPrice
        this.model.lmtUsedFlag = val[0].lmtUsedFlag
        this.model.lmtUsedAreas = val[0].lmtUsedAreas

        //  this.$set(this.repeatForm,'name',val[0].name)
        //   this.$set(this.repeatForm,'manufacturer',val[0].manufacturer)
        //   this.$set(this.repeatForm,'specification',val[0].specificationDescribe)
          // this.$set(this.repeatForm,'originPlace',val[0].originPlace)
          
          
        if(this.model.id){
          this.model.medicalCatalogCode = val[0].medicalCatalogCode
          this.model.medicalCatalogName = val[0].medicalCatalogName
          this.form.setFieldsValue(pick(val[0],'approvalNumber','miLevel','mnemonic'))
        }else{
          this.model.medicalCatalogCode = val[0].medicalCatalogCode
          this.model.code = val[0].medicalCatalogCode
          this.model.medicalCatalogName = val[0].medicalCatalogName
          this.form.setFieldsValue(pick(val[0],'lmtUsedAreas','trdnFlag','noTraceFlag','identificationCode','tracePackageQuantity','approvalNumber','name','retailPrice','specification','specificationDescribe','dose','startDate','endDate','manufacturer','dosageForm','doseUnit','packUnit','prescriptionFlag','miLevel','packageQuantity','mnemonic'))
         
        }
        this.setTimeNext(0)
       
      },
      ybdzChange(val){
        console.log(val);
        if(val == 'Y'){
          this.ybdz = true;
          this.form.setFieldsValue({'approvalNumber':''})
        }else{
          this.ybdz = false;
        }
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.noSymChange(record.noTraceFlag)
        // this.$set(this.model,'markupRatioFlag',record.markupRatioFlag == 1?true:false)
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'trdnFlag','noTraceFlag','identificationCode','tracePackageQuantity','markupRatio','name','mnemonic','code','retailPrice','packUnit','approvalNumber','dose','doseUnit','startDate','endDate','manufacturer','unit','specification','dosageForm','usage','frequency','administration','splitFlag','prescriptionFlag','enabledFlag','medicalCatalogCode','delFlag','createBy','createTime','updateBy','updateTime','tenantId','singleCommission','specificationDescribe','miLevel','purchaseUnitPrice','packageQuantity','stockLimitUp','stockLimitDown'))
          if(!record.id){
            this.getCode()
            // 获取当前日期
            const today = new Date();
            // 增加年份，5为5年
            today.setFullYear(today.getFullYear() + 5);

            this.form.setFieldsValue({'endDate':today.toISOString().substring(0, 10)})
            //默认 不开启限价
            this.$set(this.model,'markupRatioFlag','0')
          }
          this.chailingChange(this.model.trdnFlag)
         
         
        })
      },
      //渲染流程表单数据
      showFlowData(){
        
        if(this.formBpm === true && this.formData.dataId){
         
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
              this.resetKeydown()
            }
          });
        }else{
          this.resetKeydown()
        }
      },
    
      
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','mnemonic','code','packUnit','retailPrice','approvalNumber','dose','doseUnit','startDate','endDate','manufacturer','unit','specification','dosageForm','usage','frequency','administration','splitFlag','prescriptionFlag','enabledFlag','medicalCatalogCode','delFlag','createBy','createTime','updateBy','updateTime','tenantId','lmtPrice','lmtUsedFlag'))
      },
    }
  }
</script>
<style lang="less" scoped>
.xianjiainfo{
  display: flex;
  gap:20px;
  div{
    display: flex;
  }
  .red{
    color: #FF0000;
  }
}

.top-yb{
  display: flex;
  align-items: center;
  padding-bottom: 8px;
  flex-wrap: wrap;
  h3{
    margin-bottom: 0;
    margin-right: 32px;
  }
  /deep/ .ant-form-item{
    margin-bottom: 0;
    display: flex;
    margin-right: 18px;
  }
  border-bottom: 1px solid #D9D9D9;
  margin-bottom: 20px;
  &>p{
    flex:1;
    color: #FF0000;
  }
}
.yb-form{
  background: #F0F0F0;
  padding: 12px;
}
</style>